package cn.rwklyd.ecommerce.service;

import cn.rwklyd.ecommerce.mapper.AdminMapper;
import cn.rwklyd.ecommerce.mapper.UserMapper;
import cn.rwklyd.ecommerce.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 键盘书生
 * @date 2024/12/3
 */
@Service
@Slf4j
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    //      查询商品信息
    public List<Product> productList() {
        return adminMapper.productList();
    }

    //    增加商品信息
    public boolean addProduct(Product product) {
        return adminMapper.addProduct(product);
    }

    //    删除商品信息
    public boolean deleteProduct(String productId) {
        return adminMapper.deleteProduct(productId);
    }

    //    修改商品信息
    public boolean updateProduct(Product product) {
        return adminMapper.updateProduct(product);
    }

    //    查询顾客信息
    public List<Users> queryUserList() {
        return adminMapper.queryUserList();
    }

    //  查询订单信息
    public List<Cart> queryOrderList() {
        List<Cart> cartList = adminMapper.queryOrderList();
        for (Cart cart : cartList){
            cart.setProduct(userMapper.getProductById(String.valueOf(cart.getProductId())));
        }
        return cartList;
    }

    //   根据用户ID查询用户日志
    public List<LogAnalysis> queryUserLog(String id) {
        List<LogAnalysis> logAnalysisList = new ArrayList<>();
        List<UserLog> userLogs = adminMapper.queryUserLog(id);  // 查询用户日志
        log.info("用户日志：" + userLogs);
        // 根据 userID 查询用户信息
        Users user = userMapper.getUserById(id);
        String username = user.getUsername(); // 获取用户名
        // 遍历每个 UserLog 并转换为 LogAnalysis
        for (UserLog userLog : userLogs) {
            LogAnalysis logAnalysis = new LogAnalysis();
            logAnalysis.setUsername(username); // 设置用户名
            logAnalysis.setActionType(userLog.getActionType()); // 设置操作类型
            // 直接将操作描述赋值为操作内容
            logAnalysis.setAction(userLog.getActionDescription());
            logAnalysis.setActionTime(userLog.getActionTime()); // 设置操作时间
            logAnalysis.setSuccess(userLog.getSuccess() ? "成功" : "失败"); // 设置操作结果

            logAnalysisList.add(logAnalysis);
        }
        return logAnalysisList;
    }


}
